import {createRouter, createWebHashHistory} from "vue-router";
const loginPage = () => import("../page/LoginPage.vue")
const IndexPage = () => import("../page/pc/IndexPage.vue")
const aIndexPage = () => import("../page/admin/IndexPage.vue")
//管理端
const userManagePage = () => import("../page/admin/page/UserManagePage.vue")
const schoolManagePage = () => import("../page/admin/page/SchoolManagePage.vue")
const roleManagePage = () => import("../page/admin/page/RoleManagePage.vue")
const apiLogManagePage = () => import("../page/admin/page/ApiLogManagePage.vue")
const taskJobManagePage = () => import("../page/admin/page/TaskJobManagePage.vue")
const scheduleManagePage = () => import("../page/admin/page/ScheduleManagePage.vue")
const subjectManagePage = () => import("../page/admin/page/SubjectManagePage.vue")
const scoreRuleManagePage = () => import("../page/admin/page/ScoreRuleManagePage.vue")

//教师端
const stuManagePage = () => import("../page/pc/page/StuManagePage.vue")
const stuInfoManagePage = () => import("../page/pc/page/StuInfoManagePage.vue")
const scoreManagePage = () => import("../page/pc/page/ScoreManagePage.vue")
const classManagePage = () => import("../page/pc/page/ClassManagePage.vue")
const scoreCollectManagePage = () => import("../page/pc/page/ScoreCollectManagePage.vue")
const pcUserManagePage = () => import("../page/pc/page/UserManagePage.vue")

const homePage = () => import("../page/pc/page/HomePage.vue")
const routes = [
    {path: "/", redirect: "/login"},
    {path: "/adminIndex", redirect: "/adminIndex/apiLogManage"},
    {path: "/pcIndex", redirect: "/pcIndex/home"},
    {path: "/login", component: loginPage},
    {
        path: "/pcIndex",
        component: IndexPage,
        children: [
            {path: "/pcIndex/home", component: homePage},
            {path: "/pcIndex/classManage", component: classManagePage},
            {path: "/pcIndex/stuManage", component: stuManagePage},
            {path: "/pcIndex/stuInfoManage", component: stuInfoManagePage},
            {path: "/pcIndex/scoreManage", component: scoreManagePage},
            {path: "/pcIndex/scoreCollectManage", component: scoreCollectManagePage},
            {path: "/pcIndex/userManage", component: pcUserManagePage},
        ]
    },
    {
        path: "/adminIndex",
        component: aIndexPage,
        children: [
            {path: "/adminIndex/userManage", component: userManagePage},
            {path: "/adminIndex/schoolManage", component: schoolManagePage},
            {path: "/adminIndex/roleManage", component: roleManagePage},
            {path: "/adminIndex/apiLogManage", component: apiLogManagePage},
            {path: "/adminIndex/taskJobManage", component: taskJobManagePage},
            {path: "/adminIndex/scheduleManage", component: scheduleManagePage},
            {path: "/adminIndex/subjectManage", component: subjectManagePage},
            {path: "/adminIndex/scoreRuleManage", component: scoreRuleManagePage},
        ]
    },
    {
        name: '登录',
        path: "/login", component: loginPage,
    },
    {
        path:'/:pathMatch(.*)',
        component:()=>import('../page/404page.vue')
    },
]

export const router = createRouter({
    history: createWebHashHistory(),
    routes: routes,
    mode: 'history',
})

router.beforeEach((to, from, next) => {
    localStorage.setItem("currentPathName", to.name)
    if (to.path === '/login') {
        return next()
    }
    var localToken = window.localStorage.getItem('token');
    console.info(localToken)
    if (!localToken) {
        console.log("login")
        return next('/login')
    }
    if (to.path === '/index') {
        let roles = JSON.parse(window.localStorage.getItem('user'));
        console.log(roles)
        let isAdmin = roles.rank.includes('superAdmin');
        if (isAdmin) {
            return next('/adminIndex')
        } else {
            return next('/pcIndex')
        }
    }

    next()
})

export default router
